#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> c(n + 1), x(n + 1);
  for (int i = 1; i <= n; i ++) cin >> c[i];
  for (int i = 1; i <= n; i ++) cin >> x[i];
  c.insert(c.end(), c.begin() + 1, c.end());
  n *= 2;
  vector<vector<i64>> dp(n + 2, vector<i64>(n + 2));

  for (int l = n; l >= 1; l --) {
    dp[l][l] = 1 + x[c[l]];
    for (int r = l + 1; r <= n; r ++) {
      dp[l][r] = LLONG_MAX / 2;
      for (int k = l; k < r; k ++) {
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
      }

      if (c[l] == c[r]) {
        for (int k = l; k < r; k ++) {
          if (c[k] == c[l]) {
            dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r - 1] + r - k);
          }
        }
      }
    }
  }
  n /= 2;
  i64 ans = LLONG_MAX;
  for (int i = 1; i <= n; i ++) {
    ans = min(ans, dp[i][i + n - 1]);
  }
  cout << ans << endl;
}